﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_Person_VCard : BaseTemplatePage
{
    protected override void AttachEvents()
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PostControl positionControl = AcmsContext.CurrentPost.FindPostControl("Position");
            TextContentControlProperties positionProp = null;
            if (positionControl != null)
            {
                positionProp = positionControl.GetProperties<TextContentControlProperties>();
            }
            PostControl telControl = AcmsContext.CurrentPost.FindPostControl("Tel");
            TextContentControlProperties telProp = null;
            if (telControl != null)
            {
                telProp = telControl.GetProperties<TextContentControlProperties>();
            }
            PostControl emailControl = AcmsContext.CurrentPost.FindPostControl("Email");
            TextContentControlProperties emailProp = null;
            if (positionControl != null)
            {
                emailProp = emailControl.GetProperties<TextContentControlProperties>();
            }

            TextContentControlProperties faxProp = null;
            TextContentControlProperties addressProp = null;
            TextContentControlProperties postcodeProp = null;
            TextContentControlProperties townProp = null;
            //Find the Office Post 
            List<Topic> topicOfiices = Topic.GetAllByPostIdAndTopicGroupPath(AcmsContext.PostId, "Offices");
            if (topicOfiices != null && topicOfiices.Count > 0)
            {
                Post office = Post.GetBySiteIdAndPath(AcmsContext.SiteId, string.Concat("~/Offices/", topicOfiices[0].Name), AcmsContext.SiteVersion);
                if (office != null)
                {
                    PostControl faxControl = office.FindPostControl("Fax");
                    if (faxControl != null)
                    {
                        faxProp = faxControl.GetProperties<TextContentControlProperties>();
                    }
                    PostControl addressControl = office.FindPostControl("Address");
                    if (addressControl != null)
                    {
                        addressProp = addressControl.GetProperties<TextContentControlProperties>();
                    }
                    PostControl postCodeControl = office.FindPostControl("PostCode");
                    if (postCodeControl != null)
                    {
                        postcodeProp = postCodeControl.GetProperties<TextContentControlProperties>();
                    }
                    PostControl townControl = office.FindPostControl("Town");
                    if (townControl != null)
                    {
                        townProp = townControl.GetProperties<TextContentControlProperties>();
                    }
                }
            }

            System.Text.StringBuilder tmp = new System.Text.StringBuilder();
            tmp.AppendFormat("BEGIN:VCARD{0}", Environment.NewLine);
            tmp.AppendFormat("VERSION:2.1{0}", Environment.NewLine);
            tmp.AppendFormat("N:{0};{1};", AcmsContext.CurrentPost.DisplayName, Environment.NewLine);
            tmp.AppendFormat("FN:{0}{1}", AcmsContext.CurrentPost.DisplayName, Environment.NewLine);
            if(positionProp != null && !string.IsNullOrEmpty(positionProp.Text))
            {
                tmp.AppendFormat("TITLE:{0}{1}", positionProp.Text, Environment.NewLine);
            }
            tmp.AppendFormat("ORG:Bevans{0}", Environment.NewLine);
            tmp.AppendFormat("URL;WORK:{0}{1}", string.Concat("http://", Request.ServerVariables["Server_Name"],  ResolveUrl(AcmsContext.CurrentPost.Url)), Environment.NewLine);
            if(emailProp != null && !string.IsNullOrEmpty(emailProp.Text))
            {
                tmp.AppendFormat("EMAIL;PREF;INTERNET:{0}{1}", emailProp.Text, Environment.NewLine);
            }
            if(telProp != null && !string.IsNullOrEmpty(telProp.Text))
            {
                tmp.AppendFormat("TEL;WORK;VOICE:{0}{1}", telProp.Text, Environment.NewLine);
            }
            if (faxProp != null && !string.IsNullOrEmpty(faxProp.Text))
            {
                tmp.AppendFormat("TEL;WORK;FAX:{0}{1}", faxProp.Text, Environment.NewLine);
            }
            if (addressProp != null && !string.IsNullOrEmpty(addressProp.Text) && townProp != null && !string.IsNullOrEmpty(townProp.Text) && postcodeProp != null && !string.IsNullOrEmpty(postcodeProp.Text))
            {
                System.Text.StringBuilder tmpDisplayAddress = new System.Text.StringBuilder();
                tmpDisplayAddress.AppendFormat(addressProp.Text.Replace(",", ","));
                tmpDisplayAddress.AppendFormat(";{0};;{1};United Kingdom{2}", townProp.Text, postcodeProp.Text, Environment.NewLine);

                tmp.AppendFormat("ADR;WORK;PREF:;;{0}{1}", tmpDisplayAddress.ToString(), Environment.NewLine);
                System.Text.StringBuilder tmpPrintAddress = new System.Text.StringBuilder();
                tmpPrintAddress.AppendFormat(addressProp.Text.Replace(",", ",=0D=0"));
                if (!tmpPrintAddress.ToString().EndsWith("=0D=0"))
                {
                    tmpPrintAddress.Append("=0D=0");
                }
                tmpPrintAddress.AppendFormat("{0}=0D=0 {1}{2}", townProp.Text, postcodeProp.Text, Environment.NewLine);
                tmp.AppendFormat("LABEL;WORK;ENCODING=QUOTED-PRINTABLE:{0}", tmpPrintAddress.ToString());
            }
            if(AcmsContext.CurrentPost.PublishFrom.HasValue)
            {
                tmp.AppendFormat("REV:{0}{1}", AcmsContext.CurrentPost.PublishFrom.Value.ToUniversalTime().ToString("yyyyMMdd HHmmss"), Environment.NewLine);
            }
            tmp.AppendFormat("END:VCARD{0}", Environment.NewLine);

            Page.Trace.IsEnabled = false;
            Page.Response.ClearHeaders();
            Page.Response.Clear();
            Page.Response.ContentType = "text/x-vcard";
            Page.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.{1}", AcmsContext.CurrentPost.DisplayName, "vcf"));
            Page.Response.Write(tmp.ToString());
            Page.Response.Flush();
            Page.Response.End();

        }
    }
}
